<?php

class Mtaikhoan extends CI_Model {

    private $matk;
    private $Status_matk;
    private $tentk;
    private $Status_tentk;
    private $matkhau;
    private $Status_matkhau;
    private $email;
    private $Status_email;
    private $diachi;
    private $Status_diachi;
    private $maloaitk;
    private $Status_maloaitk;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->Oncreate();
    }

    private function Oncreate() {
        $this->matk = "";
        $this->Status_matk = 0;
        $this->tentk = "";
        $this->Status_tentk = 0;
        $this->matkhau = "";
        $this->Status_matkhau = 0;
        $this->email = "";
        $this->Status_email = 0;
        $this->diachi = "";
        $this->Status_diachi = 0;
        $this->maloaitk = "";
        $this->Status_maloaitk = 0;
    }

    public function set_matk($data) {
        $this->matk = $data;
        $this->Status_matk = 1;
    }

    public function set_tentk($data) {
        $this->tentk = $data;
        $this->Status_tentk = 1;
    }

    public function set_matkhau($data) {
        $this->matkhau = $data;
        $this->Status_matkhau = 1;
    }

    public function set_email($data) {
        $this->email = $data;
        $this->Status_email = 1;
    }

    public function set_diachi($data) {
        $this->diachi = $data;
        $this->Status_diachi = 1;
    }

    public function set_maloaitk($data) {
        $this->maloaitk = $data;
        $this->Status_maloaitk = 1;
    }

    public function get_matk() {
        return $this->matk;
    }

    public function get_tentk() {
        return $this->tentk;
    }

    public function get_matkhau() {
        return $this->matkhau;
    }

    public function get_email() {
        return $this->email;
    }

    public function get_diachi() {
        return $this->diachi;
    }

    public function get_maloaitk() {
        return $this->maloaitk;
    }

    /**
     *
     * Get  Oject
     *
     * @strWhere    Mệnh đề where
     * @return     no return. đưa các giá trị vào object gọi hàm()
     *
     */
    public function getObject($strWhere) {
        $this->Oncreate();
        $this->db->where($strWhere, NULL, FALSE);
        $query = $this->db->get("taikhoan");
        $ar = $query->row_array();
        if (!empty($ar)) {
            $this->matk = $ar["matk"];
            $this->tentk = $ar["tentk"];
            $this->matkhau = $ar["matkhau"];
            $this->email = $ar["email"];
            $this->diachi = $ar["diachi"];
            $this->maloaitk = $ar["maloaitk"];
        }
    }

    /**
     *
     * Insert Object into database
     *
     */
    public function Save() {
        $data = array("matk" => $this->matk, "tentk" => $this->tentk, "matkhau" => $this->matkhau, "email" => $this->email, "diachi" => $this->diachi, "maloaitk" => $this->maloaitk);
        $this->db->insert("taikhoan", $data);
    }

    /**
     *
     * Update Object có các field sử dụng hàm set_()
     *
     * @strWhere=null update tất cả các row trong database
     *
     */
    public function UpdateField($strWhere = NULL) {
        $arr = array();
        if ($this->Status_matk == 1)
            $arr["matk"] = $this->matk;
        if ($this->Status_tentk == 1)
            $arr["tentk"] = $this->tentk;
        if ($this->Status_matkhau == 1)
            $arr["matkhau"] = $this->matkhau;
        if ($this->Status_email == 1)
            $arr["email"] = $this->email;
        if ($this->Status_diachi == 1)
            $arr["diachi"] = $this->diachi;
        if ($this->Status_maloaitk == 1)
            $arr["maloaitk"] = $this->maloaitk;
        if (!empty($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        $this->db->update("taikhoan", $arr);
    }

    /**
     *
     * Get list Oject
     *
     * @strFieldName         Các field cần lấy dữ liệu
     * @strWhere             Mệnh đề where
     * @strOrderby           Ex: 'title desc, name asc'
     * @Limit_Number_Row     Số dòng dữ liệu cần lấy ra
     * @Limit_Start_Index    Vị trí bắt đầu lấy dữ liệu
     *     foreach ($t as $value) {
      $ob=(object)($value);
      echo $ob->taikhoanname;
      echo $ob->pass;
      }
     * @return               result_object();
     *
     */
    public function getListObject($strFieldName = NULL, $strWhere = NULL, $strOrderby = NULL, $Limit_Number_Row = NULL, $Limit_Start_Index = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        if (!is_null($strFieldName)) {
            $this->db->select($strFieldName);
        }
        if (!is_null($strOrderby)) {
            $this->db->order_by($strOrderby);
        }
        if (!is_null($Limit_Number_Row)) {
            if (!is_null($Limit_Start_Index))
                $this->db->limit($Limit_Number_Row, $Limit_Start_Index);
            else {
                $this->db->limit($Limit_Number_Row);
            }
        }
        $query = $this->db->get("taikhoan");
        return $query->result_object();
    }

    /**
     *
     * Get list Oject
     *
     * @strWhere    Mệnh đề where. Null sẽ xóa hết dữ liệu của table
     * @return      no
     *
     */
    public function Delete($strWhere = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
            $this->db->delete("taikhoan");
        }
        else
            $this->db->empty_table('taikhoan');
    }

    /**
     *
     * Lây max giá trị kiểu int của 1 trường dữ liệu
     *
     * @strFieldName    Trường dữ liệu cần lấy max(1 trường duy nhất)
     * @strWhere        Mệnh đề where
     * @return          1 giá trị kiểu string
     *
     */
    public function Max($strFieldName, $strWhere = NULL) {
        if (!is_null($strWhere))
            $this->db->where($strWhere, NULL, FALSE);
        $this->db->select_max($strFieldName);
        $query = $this->db->get('taikhoan');
        $ob = $query->row_array();
        return $ob[$strFieldName];
    }

    public function dangnhap($email, $pass) {
        $bool = false;
        $this->db->select("*");
        $this->db->where("email", $email);
        $this->db->where("matkhau", $pass);
        $this->db->from("taikhoan");
        //$this->db->get()->result();
        //echo($this->db->last_query());
        $ar = $this->db->get()->row_array();
        if (!empty($ar)) {
            $bool = TRUE;          
            $this->matk = $ar["MATK"];
            $this->tentk = $ar["TENTK"];
            $this->matkhau = $ar["MATKHAU"];
            $this->email = $ar["EMAIL"];
            $this->diachi = $ar["DIACHI"];
            $this->maloaitk = $ar["MALOAITK"];
            $data = array("is_login" => $bool, "matk" => $this->matk, "tentk" => $this->tentk, "email" => $this->email, "maloaitk" => $this->maloaitk);
            $this->session->set_userdata($data);
        }
        return $bool;
    }

}

?>